package com.tiansu.reborn.wanda.modules.executor.controller;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.tiansu.reborn.wanda.modules.executor.dao.TVDeviceDao;
import com.tiansu.reborn.wanda.modules.executor.entity.CWRechargeEntity;
import com.tiansu.reborn.wanda.modules.executor.entity.RechargeEntity;
import com.tiansu.reborn.wanda.modules.executor.entity.TVDeviceEntity;
import com.tiansu.reborn.wanda.modules.executor.entity.vo.RechargeVo;
import com.tiansu.reborn.wanda.modules.executor.utils.OkHttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName RechargeRetryController
 * @Description : 商服充值失败的充值
 *   业务比较简单  直接业务处理在Controller 处理 不建议学习
 * @param
 * @Return :
 * @Author : ldd
 * @Date : 2020/9/10 15:37
*/
@Controller
public class RechargeRetryController {

    @Autowired
    private TVDeviceDao tvDeviceDao;


    @Value("${my.url}")
    private String url;


    @GetMapping("/recharge_retry")
    public String RechargeRetry(){
        return "recharge_retry";
    }

    @ResponseBody
    @PostMapping("/recharge_retry_submit")
    public Boolean submit(RechargeVo rechargeVo){
        TVDeviceEntity tvDeviceEntity = tvDeviceDao.selectById(rechargeVo.getDeviceId());
        RechargeEntity rechargeEntity=new RechargeEntity();
        rechargeEntity.setDetailsId(rechargeVo.getDetailsId());
        rechargeEntity.setAmount(rechargeVo.getMoney().toString());
        rechargeEntity.setBerthNumber(tvDeviceEntity.getNickname());
        rechargeEntity.setBrandId(tvDeviceEntity.getBrandcode());
        rechargeEntity.setPlazaId(tvDeviceEntity.getPlazaid().toString());
        rechargeEntity.setMeterNumber(rechargeVo.getDeviceId().toString());
        Map<String, String> map = new HashMap<>();
        Gson g = new GsonBuilder().create();

        // 一次只处理一条
        map.put("Inputs",g.toJson(rechargeEntity));
        String result = OkHttpUtil.postFormParams(url, map);
        return true;



    }



    @ResponseBody
    @PostMapping("/cwrecharge_retry_submit")
    public Boolean cw_submit(RechargeVo rechargeVo){
        TVDeviceEntity tvDeviceEntity = tvDeviceDao.selectById(rechargeVo.getDeviceId());
        CWRechargeEntity rechargeEntity=new CWRechargeEntity();
        rechargeEntity.setDetailsId(rechargeVo.getDetailsId());
        rechargeEntity.setAmount(rechargeVo.getMoney().toString());
        rechargeEntity.setBerthNumber(tvDeviceEntity.getNickname());
        rechargeEntity.setBrandId(tvDeviceEntity.getBrandcode());
        rechargeEntity.setPlazaId(tvDeviceEntity.getPlazaid().toString());
        rechargeEntity.setMeterNumber(rechargeVo.getDeviceId().toString());

        Map<String, String> map = new HashMap<>();
        Gson g = new GsonBuilder().create();

        // 一次只处理一条
        map.put("Inputs",g.toJson(rechargeEntity));
        String result = OkHttpUtil.postFormParams(url, map);
        return true;



    }

}
